#include "datacenter.h"
#include "clientcenter.h"
#include "clientgroupcenter.h"
#include "databaseexception.h"
#include "networkstatus.h"
#include "volumecenter.h"
#include "contractcenter.h"
#include "playlistcenter.h"
#include "filetransferlogcenter.h"
#include "unnormalstatecenter.h"
#include <accountmanage.h>

#include <QDebug>
#include <QSqlError>

using boost::shared_ptr;

DataCenter::DataCenter(const QString &hostName,int tcpPort,
					   const QString &dbName,
					   const QString &username,
					   const QString& password,
					   const QString& driverType)
						   : m_Database(QSqlDatabase::addDatabase(driverType))
{

//	QString dsn = "DRIVER={SQL Server};"
//						  "SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;";
//
//	dsn = dsn.arg(hostName, dbName, username, password);
//	m_Database.setDatabaseName(dsn);
	m_Database.setHostName(hostName);
	m_Database.setDatabaseName(dbName);
	m_Database.setPort(tcpPort);
	m_Database.setUserName(username);
	m_Database.setPassword(password);
	if(!m_Database.open())
	{
		qDebug()<<m_Database.lastError();
		throw DatabaseException(m_Database.lastError().text(), m_Database);
	}
}

shared_ptr<ClientCenter> DataCenter::createClientCenter()const
{
	static shared_ptr<ClientCenter> clientCenter( new ClientCenter(*this));
	return clientCenter;
}

shared_ptr<ClientGroupCenter> DataCenter::createClientGroupCenter() const
{
	static shared_ptr<ClientGroupCenter> clientGroupCenter(new ClientGroupCenter(*this) );
	return clientGroupCenter;
}

NetworkStatusCenter* DataCenter::createNetworkStatusCenter() const
{
	static NetworkStatusCenter* networkStatusCenter = new NetworkStatusCenter(*this);
	return networkStatusCenter;
}

VolumeCenter* DataCenter::createVolumeCenter() const
{
	static VolumeCenter* volumeCenter = new VolumeCenter(*this);
	return volumeCenter;
}

ContractCenter* DataCenter::createContractCenter() const
{
	static ContractCenter* contractCenter = new ContractCenter(*this);
	return contractCenter;
}

PlayListCenter* DataCenter::createPlayListCenter() const
{
	static PlayListCenter* playListCenter = new PlayListCenter(*this);
	return playListCenter;
}

FileTransferLogCenter* DataCenter::createFileTransferLogCenter() const
{
	static FileTransferLogCenter* fileLogCenter = new FileTransferLogCenter(*this);
	return fileLogCenter;
}

UnnormalStateCenter* DataCenter::createUnnormalStateCenter() const
{
	static UnnormalStateCenter* unnormalStateCenter = new UnnormalStateCenter(*this);
	return unnormalStateCenter;
}

AccountManage* DataCenter::createAccountManage() const
{
	static AccountManage* accountManage = new AccountManage(*this);
	return accountManage;
}
